package com.aceitunaproject.server.test;

import net.java.dev.mocksgs.MockSGS;

import org.junit.Assert;

import com.aceitunaproject.server.AceitunaContext;
import com.aceitunaproject.server.ServerConfiguration;
import com.aceitunaproject.server.initializer.Initializer;
import com.aceitunaproject.server.initializer.TestInitializer;
import com.aceitunaproject.server.terrain.TerrainManager;

/**
 * 
 * Helper class for testing within the Darkstar/Aceituna framework
 * 
 * @author Federico Bergstein
 * 
 */
public class AceitunaTestHelper {

	private Initializer defaultInitializer = new TestInitializer();
	private String application_name = "Aceituna Test";

	/**
	 * Gets the current Darkstar Server Application name being used by the
	 * helper
	 * 
	 * @return Application name being used by the helper
	 */
	public String getApplication_name() {
		return application_name;
	}

	/**
	 * Sets the Darkstar Server Application name to be used by the helper
	 * 
	 * @param application_name
	 *            the Application Name
	 */
	public void setApplication_name(String application_name) {
		this.application_name = application_name;
	}

	/**
	 * 
	 * Runs a test case wrapped into an AceitunaTest class
	 * 
	 * @param test
	 *            the Aceituna Test to run.
	 * @param initializer
	 *            the Initializer Class to set up the game world for the test.
	 * @throws Exceptions
	 */
	public void runTest(AceitunaTest test, Initializer initializer) {

		TerrainManager.reset();
		MockSGS.init();
		AceitunaContext.initializeManagers();
		ServerConfiguration.SetInitializer(initializer);
		initializer.Initialize();
		try {

			// Execute the task
			MockSGS.run(test);

		} catch (Exception ex) {
			Assert.fail(ex.getCause().getMessage());

		} finally {
			MockSGS.reset();
		}
	}

	/**
	 * Runs a test case wrapped into an AceitunaTest class, using the helper's
	 * default initializer
	 * 
	 * @param test
	 *            the Aceituna Test to run.
	 * @throws Exception
	 */
	public void runTest(AceitunaTest test) {
		runTest(test, defaultInitializer);
	}

	/**
	 * Sets a default {@link Initializer} for the class instance
	 * 
	 * @param defaultInitializer
	 *            the Initializer to set
	 */
	public void setDefaultInitializer(Initializer defaultInitializer) {
		this.defaultInitializer = defaultInitializer;
	}

	/**
	 * @return the current default {@link Initializer}
	 */
	public Initializer getDefaultInitializer() {
		return defaultInitializer;
	}

}
